package com.ljx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.common.AppHttpCodeEnum;
import com.ljx.common.ResponseResult;
import com.ljx.domain.entity.Comment;
import com.ljx.domain.entity.User;
import com.ljx.domain.vo.CommentVo;
import com.ljx.domain.vo.PageVo;
import com.ljx.exception.SystemException;
import com.ljx.mapper.CommentMapper;
import com.ljx.service.CommentService;
import com.ljx.service.UserService;
import com.ljx.utils.BeanCopyUtils;
import com.ljx.utils.SystemConstants;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 评论表(SgComment)表服务实现类
 *
 * @author makejava
 * @since 2023-04-19 16:22:58
 */
@Service("CommentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(String commentType,Long articleId,
                                      Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //根据文章的id查询
        queryWrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        //查询根评论
        queryWrapper.eq(Comment::getRootId,-1);
        queryWrapper.eq(Comment::getType,commentType);
        Page<Comment> page = new Page(pageNum,pageSize);
        page(page,queryWrapper);
        List<Comment> list = page.getRecords();
        List<CommentVo> commentVos = toCommentVoList(list);
        for (CommentVo commentVo : commentVos) {
            //查询对应的子评论
            List<CommentVo> children = getChildren(commentVo.getId());
            //赋值
            commentVo.setChildren(children);
        }
        return ResponseResult.okResult(new PageVo(commentVos,page.getTotal()));
    }

    /**
     * 添加评论
     * @param comment
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.SYSTEM_ERROR);
        }
        save(comment);
        return ResponseResult.okResult(comment);
    }

    /**
     * 根据根评论的id查询所对应的子评论的集合
     * @param id 根评论的id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {

        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> comments = list(queryWrapper);
        List<CommentVo> commentVos = toCommentVoList(comments);
        return commentVos;
    }

    /**
     * 设置评论的作者
     * @param list
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = list.stream().map(mvp -> {
            CommentVo commentVo = new CommentVo();
            BeanUtils.copyProperties(mvp, commentVo);
            //String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            User user = userService.getById(commentVo.getCreateBy());
            String nickName = user.getNickName();
            String avatar = user.getAvatar();
            commentVo.setUsername(nickName);
            commentVo.setAvatar(avatar);
            if(commentVo.getToCommentUserId() != -1){
               // String name = userService.getById(commentVo.getToCommentUserId()).getNickName();
                User user1 = userService.getById(commentVo.getToCommentUserId());
                commentVo.setToCommentUserName(user1.getNickName());
                commentVo.setToCommentAvatar(user.getAvatar());
            }
            return commentVo;
        }).collect(Collectors.toList());
        return commentVos;
    }

}
